package com.lz.football.module.techBar.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.greensock.TweenLite;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.lz.football.module.techBar.item.TechPlaceItem;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.events.ListEvent;

	/**
	 *角色转职 
	 * @author lake
	 * 
	 */	
	public class RoleChangePanel extends TechBasePanel implements ITechPanel
	{
		/**
		 *球员卡容器 
		 */		
		private var playerCardContainer:MovieClip;
		/**
		 *开始
		 */		
		private var startBtn			:SimpleButton;
		/**
		 *需要欧元 
		 */		
		private var txtPrice			:TextField;
		/**
		 *洗练价格
		 * 灰 -- 黑 
		 */		
		private var prices				:Array = [0,10000,20000,40000,500000,2000000,10000000,100000000];
		/**
		 *规则 
		 */		
		private var ruleBtn			:SimpleButton;
		/**
		 *球员卡 
		 */		
		private var playerCard 		:*;
		/**
		 *三种位置 
		 */		
		private var itemList			:Array = [];
		/**
		 *选择的item 
		 */		
		private var selectItem			:TechPlaceItem;
		/**
		 *自己角色 
		 */		
		private var selfVo 			:PlayerStarVo;
		
		public function RoleChangePanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		override public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.RoleChangeSkin");
			this.addChild(_symbolSkin);
			startBtn = _symbolSkin.startBtn;
			ruleBtn = _symbolSkin.ruleBtn;
			txtPrice = _symbolSkin.txtPrice;
			playerCardContainer = _symbolSkin.playerCardContainer;
			//球员卡
			playerCard = Reflection.createInstance("com.liangzhong.view.tips.PlayerCardTips");
			selfVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();
			playerCard.cdata = selfVo;
			playerCardContainer.addChild(playerCard);
			txtPrice.text = prices[selfVo.color - 1];
			//初始化item
			var i :int = 0;
			var tItem :TechPlaceItem;
			var places :Array = [4,3,2];
			for(;i<3;i++)
			{
				tItem = new TechPlaceItem();
				tItem.initUI(_symbolSkin["item_" + i]);
				tItem.data = places[i];
				itemList.push(tItem);
				if(places[i] == selfVo.place)
				{
					selectItem = tItem;
					selectItem.select();
				}
				tItem.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler)
			}
			//事件
			addEvtListener();
			//效果
			effectMc = _symbolSkin.effectMc;
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
			//特效球员信息
			if(effectMc)
			{
				effectMc.pmc.mc.txtName.text = "" + selfVo.name;
				//大头像
				var bigImgBg :MovieClip = effectMc.pmc.mc.bigImgBg;
				var bigBData :BitmapData = selfVo.bigBitmapData;
				var bigBitMap :Bitmap = new Bitmap(bigBData);
				DisplayObjectUtil.removeAllChildren(bigImgBg);
				if(bigImgBg)
				{
					bigImgBg.addChild(bigBitMap);
				}
			}
		}
		/**
		 *注册事件 
		 * 
		 */	
		override public function addEvtListener():void
		{
			startBtn.addEventListener(MouseEvent.CLICK,startClickHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,ruleClickHandler);
		}
		/**
		 *规则点击 
		 * @param e
		 * 
		 */		
		override protected function ruleClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new TechBarEvent("rule_Click",6,true));
		}
		/**
		 *点击左边物品 
		 * @param e
		 * 
		 */		
		private function showItemClickHandler(e :ListEvent):void
		{
			var goodsVo :GoodsVo = e.item as GoodsVo;
			if(goodsVo == null)
			{
				return;
			}
			txtPrice.text = "" + prices[goodsVo.color - 1];
			playerCard.setPureData(goodsVo,playerCard.getChildAt(0));
		}
		/**
		 * 选择item  
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :Event):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
			}
			selectItem = e.currentTarget as TechPlaceItem;
			selectItem.select();
		}
		/**
		 *重设 恢复初始化 
		 * 
		 */		
		override public function reset():void
		{
			startBtn.mouseEnabled = true;
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
		}
		/**
		 *开始
		 * @param e
		 * 
		 */		
		private function startClickHandler(e :MouseEvent):void
		{
			var type :int = selectItem.data;
			if(selfVo.place == type)
			{
				Alert.showErrorTip("选择的球场位置没变，无需转职");
				return ;
			}
			this.dispatchEvent(new TechBarEvent(TechBarEvent.ROLE_CHANGE,{type:type},true));
			startBtn.mouseEnabled = false;
		}
		/**
		 *删除事件 
		 * 
		 */	
		override public function removeEvtListener():void
		{
			startBtn.removeEventListener(MouseEvent.CLICK,startClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		override public function destroyView():void
		{
			removeEvtListener();
			for each(var tItem :TechPlaceItem in itemList)
			{
				tItem.removeEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
				tItem.destroyView();
			}
			itemList = null;
			selfVo = null;
		}
		/////////////////////////server return//////////////////////////////////////////
		/**
		 *角色转职
		 * 
		 */		
		public function roleChange(value :Object):void
		{
			if(value.retcode == 0)
			{
				TweenLite.delayedCall(2.5,showAlertRes);
				if(effectMc)
				{
					effectMc.visible = true;
					effectMc.gotoAndPlay(1);
				}
				TweenLite.delayedCall(3,reset);
				TweenLite.delayedCall(2.5,showResObj);
			}
			else
			{
				reset();
			}
		}
		/**
		 *显示成功物品 
		 * @param pid
		 * @param ntype
		 * 
		 */		
		private function showResObj():void
		{
			selfVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();;
			playerCard.setPureData(selfVo,playerCard.getChildAt(0));
		}
		/**
		 *显示成功失败 
		 * @param value
		 * 
		 */		
		private function showAlertRes():void
		{
			Alert.showErrorTip("转职成功");
		}
	}
}